﻿using System;
using System.Text;
using PsqlDotnet.Enums.Privileges;

namespace PsqlDotnet.Model
{
    public class DbPrivileges
    {
        public string DatabaseName { get; set; }

        public DbPrivileges(string dbName, DatabasePrivilegeFlags flags)
        {
            privileges = flags;
            DatabaseName = dbName;
        }

        public DbPrivileges(string dbName, TablePrivilegeFlags flags)
        {
            privileges = flags;
            DatabaseName = dbName;
        }

        public DbPrivileges(string dbName, FunctionPrivilegeFlags flags)
        {
            privileges = flags;
            DatabaseName = dbName;
        }

        public DbPrivileges(string dbName, LanguagePrivilegeFlags flags)
        {
            privileges = flags;
            DatabaseName = dbName;
        }

        public DbPrivileges(string dbName, SchemaPrivilegeFlags flags)
        {
            privileges = flags;
            DatabaseName = dbName;
        }

        public DbPrivileges(string dbName, TablespacePrivilegeFlags flags)
        {
            privileges = flags;
            DatabaseName = dbName;
        }

        public override string ToString()
        {
            StringBuilder sb = new StringBuilder();

            if (privileges.GetType() == typeof(DatabasePrivilegeFlags))
            {
                DatabasePrivilegeFlags flags = (DatabasePrivilegeFlags)privileges;

                if ((flags & DatabasePrivilegeFlags.Connect) == DatabasePrivilegeFlags.Connect)
                    sb.Append("connect,");
                if ((flags & DatabasePrivilegeFlags.Create) == DatabasePrivilegeFlags.Create)
                    sb.Append("create,");
                if ((flags & DatabasePrivilegeFlags.Temporary) == DatabasePrivilegeFlags.Temporary)
                    sb.Append("temporary,");
            }

            if (privileges.GetType() == typeof(TablePrivilegeFlags))
            {
                TablePrivilegeFlags flags = (TablePrivilegeFlags)privileges;

                if ((flags & TablePrivilegeFlags.Delete) == TablePrivilegeFlags.Delete)
                    sb.Append("delete,");
                if ((flags & TablePrivilegeFlags.Insert) == TablePrivilegeFlags.Insert)
                    sb.Append("insert,");
                if ((flags & TablePrivilegeFlags.References) == TablePrivilegeFlags.References)
                    sb.Append("references,");
                if ((flags & TablePrivilegeFlags.Rule) == TablePrivilegeFlags.Rule)
                    sb.Append("rule,");
                if ((flags & TablePrivilegeFlags.Select) == TablePrivilegeFlags.Select)
                    sb.Append("select,");
                if ((flags & TablePrivilegeFlags.Trigger) == TablePrivilegeFlags.Trigger)
                    sb.Append("trigger,");
                if ((flags & TablePrivilegeFlags.Update) == TablePrivilegeFlags.Update)
                    sb.Append("update,");
            }

            if (privileges.GetType() == typeof(FunctionPrivilegeFlags))
            {
                FunctionPrivilegeFlags flags = (FunctionPrivilegeFlags)privileges;

                if ((flags & FunctionPrivilegeFlags.Execute) == FunctionPrivilegeFlags.Execute)
                    sb.Append("execute,");
            }

            if (privileges.GetType() == typeof(LanguagePrivilegeFlags))
            {
                LanguagePrivilegeFlags flags = (LanguagePrivilegeFlags)privileges;

                if ((flags & LanguagePrivilegeFlags.Usage) == LanguagePrivilegeFlags.Usage)
                    sb.Append("usage,");
            }

            if (privileges.GetType() == typeof(SchemaPrivilegeFlags))
            {
                SchemaPrivilegeFlags flags = (SchemaPrivilegeFlags)privileges;

                if ((flags & SchemaPrivilegeFlags.Create) == SchemaPrivilegeFlags.Create)
                    sb.Append("create,");
                if ((flags & SchemaPrivilegeFlags.Usage) == SchemaPrivilegeFlags.Usage)
                    sb.Append("usage,");
            }

            if (privileges.GetType() == typeof(TablespacePrivilegeFlags))
            {
                TablespacePrivilegeFlags flags = (TablespacePrivilegeFlags)privileges;

                if ((flags & TablespacePrivilegeFlags.Create) == TablespacePrivilegeFlags.Create)
                    sb.Append("create,");
            }




            if (sb.Length > 0 && sb[sb.Length - 1] == ',') // remove last comma if exists
                sb.Remove(sb.Length - 1, 1);

            return sb.ToString();
        }

        private Enum privileges;
    }
}
